package com.huobao.yunying.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.activitys.Act_Dailipin;
import com.huobao.yunying.activitys.Act_Kefu;
import com.huobao.yunying.activitys.Act_Progects_List;
import com.huobao.yunying.activitys.Act_Search;
import com.huobao.yunying.activitys.Act_Zhanqu;
import com.huobao.yunying.adapters.Adapter_FristPageListview;
import com.huobao.yunying.adapters.Adapter_FristPage_Grid;
import com.huobao.yunying.adapters.ImageAdapter;
import com.huobao.yunying.entity.Entry_Buttons;
import com.huobao.yunying.entity.FristPageList;
import com.huobao.yunying.entity.FristPageMingpai;
import com.huobao.yunying.entity.FristPagetodaytehui;
import com.huobao.yunying.entity.ImageBanner;
import com.huobao.yunying.entity.ZhyHot;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.huanxin.ui.ui.LoginActivity;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.ImageUtils.OnImageLoadListener;
import com.huobao.yunying.utils.LinkTypeUtils;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.yunying.views.NoScrollGridView;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.yunying.views.ObservableScrollView;
import com.huobao.yunying.views.ObservableScrollView.ScrollViewListener;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.ScreenUtil;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.xutils.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Fragment_FristPage extends Fragment implements ScrollViewListener, OnClickListener, OnItemClickListener {
	private static final int INTERFACECOUNT = 6;
	private int m_CurrentCount = 0;
	/**
	 * 轮播图相关
	 */
	private ViewFlow m_ViewFlow;
	private CircleFlowIndicator m_CircleFlowIndicator;
	private ImageAdapter imageadapter;
	/**
	 * 精品市场的5个image
	 */
	private ImageView img_1, img_2, img_3,img_4,img_5;
	private NoScrollListView listview;
	private Adapter_FristPageListview adapter, adapter_zhaoshang;
	private ObservableScrollView scrollview;
	// 标题栏
	private RelativeLayout rtly_top;
	private TextView edt_search;
	private TextView tv_search;
	/**
	 * 点击返回头部的button
	 */
	private ImageView btn_toTop;
	/**
	 * 4个按钮
	 */
	private LinearLayout Lin_1, Lin_2, Lin_3, Lin_4;
	/**
	 * 包含四个按钮的LinearLayout
	 */
	private LinearLayout Lin_Btn;

	private Http m_Http;

	/**
	 * 文字滚动广告
	 */
//	private AutoTextView m_AutoTextView;
	private TextSwitcher m_AutoTextView_new;
	/**
	 * 热点的图片布局
	 */
	private LinearLayout m_redianLayout;
	private LinearLayout llyt1, llyt2, llyt3;
	private int Hight;
	private Drawable d1, d2, d3;

	/**
	 * 母婴名牌
	 */
	private NoScrollGridView m_GridView;
	private Adapter_FristPage_Grid m_GridAdapter;

	// private int m_CurrentPage = 1;
	// private View footview;
	// private TextView footViewTextView;
	// private boolean isLoading = false;
	// 母婴名牌馆标题
	private TextView tv_mingpai_title;

	/**
	 * 轮播图容器
	 */
	private RelativeLayout rtly;

	/**
	 * 展区的imageview
	 */
	private ImageView img_zhanqu;

	private NoScrollListView listview_zhaoshang;
	private LinearLayout llyt_zhaoshang;

	private TextView tv_message;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_fristpage, null);
		m_Http = Http.getInstant();
		findViews(view);
		adapter = new Adapter_FristPageListview(getActivity());
		adapter_zhaoshang = new Adapter_FristPageListview(getActivity());
		m_GridAdapter = new Adapter_FristPage_Grid(getActivity());
		m_GridView.setAdapter(m_GridAdapter);
		m_GridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				SkipUtils.skipToSearchList(getActivity(), null, null, null, null,
						m_GridAdapter.getItem(position).getSname(), null);
			}
		});
		// listview.addFooterView(footview);
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		listview_zhaoshang.setAdapter(adapter_zhaoshang);
		listview_zhaoshang.setOnItemClickListener(this);
		// ToolLoading.ShowLoading(getActivity());
		m_AutoTextView_new.setOnClickListener(this);
		getBanber();
		getTextBanner();
		gettodaytehui();
		getMingpai();
		getList();
		getZhanqu();
		getZhaoshang();
		getTwoPic();
		return view;
	}

	private List<ImageBanner> twoPics;
	private void getTwoPic() {
		Map<String, String> map = HttpUtil.getParams("app", "default", "act", "pokerad_list");
		Http.getInstant().sendRequest(getActivity(), map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				twoPics = JSON.parseArray(jsonObject.getString("retval"), ImageBanner.class);
				m_CurrentCount++;
				checkCount(true);
				if(twoPics == null)return;
				int size = twoPics.size();
				switch(size){
					case 0:
						img_4.setVisibility(View.GONE);
						img_5.setVisibility(View.GONE);
						break;
					case 1:
						img_4.setVisibility(View.VISIBLE);
						img_5.setVisibility(View.GONE);
						ImageUtils.loadimg(img_4, twoPics.get(0).getAd_logo(), new OnImageLoadListener() {
							@Override
							public void onLoadSuccess(Drawable drawable) {
								int width = ScreenUtil.getInstant().getDisWidth(getActivity());
								LayoutParams params = (LayoutParams) img_4.getLayoutParams();
								params.width = width;
								params.height = drawable.getIntrinsicHeight() * width / drawable.getIntrinsicWidth();
								img_4.setLayoutParams(params);
							}
						});
						break;
					case 2:
						img_4.setVisibility(View.VISIBLE);
						img_5.setVisibility(View.VISIBLE);
						ImageUtils.loadimg(img_4, twoPics.get(0).getAd_logo(), new OnImageLoadListener() {
							@Override
							public void onLoadSuccess(Drawable drawable) {
								int width = (int)(ScreenUtil.getInstant().getDisWidth(getActivity()) - getResources().getDimension(R.dimen.d_20)) / 2;
								LayoutParams params = (LayoutParams) img_4.getLayoutParams();
								params.width = width;
								params.height = drawable.getIntrinsicHeight() * width / drawable.getIntrinsicWidth();
								img_4.setLayoutParams(params);
							}
						});
						ImageUtils.loadimg(img_5, twoPics.get(1).getAd_logo(), new OnImageLoadListener() {
							@Override
							public void onLoadSuccess(Drawable drawable) {
								int width = (int)(ScreenUtil.getInstant().getDisWidth(getActivity()) - getResources().getDimension(R.dimen.d_20)) / 2;
								LayoutParams params = (LayoutParams) img_5.getLayoutParams();
								params.width = width;
								params.height = drawable.getIntrinsicHeight() * width / drawable.getIntrinsicWidth();
								img_5.setLayoutParams(params);
							}
						});
						break;

				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void getZhaoshang() {
		Map<String, String> map = HttpUtil.getParams("app", "default", "act", "admore", "type", "6");
		Http.getInstant().sendRequest(getActivity(), map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				List<FristPageList> list = JSON.parseArray(jsonObject.getString("retval"), FristPageList.class);
				if (list == null || list.size() == 0) {
					llyt_zhaoshang.setVisibility(View.GONE);
					return;
				}
				llyt_zhaoshang.setVisibility(View.VISIBLE);
				adapter_zhaoshang.addAll(list);
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void getZhanqu() {
		Map<String, String> map = HttpUtil.getParams("app", "default", "act", "otherad");
		Http.getInstant().sendRequest(getActivity(), map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				try {
					JSONArray array = jsonObject.getJSONArray("retval");
					JSONObject object = array.getJSONObject(0);
					String s_pic = object.getString("s_pic");
					ImageUtils.loadimg(img_zhanqu, s_pic, new OnImageLoadListener() {

						@Override
						public void onLoadSuccess(Drawable drawable) {
							int newWidth = ScreenUtil.getInstant().getDisWidth(getActivity());
							int newHight = newWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
							LayoutParams layoutParams = (LayoutParams) img_zhanqu.getLayoutParams();
							layoutParams.width = newWidth;
							layoutParams.height = newHight;
							img_zhanqu.setLayoutParams(layoutParams);
						}
					});
					m_CurrentCount++;
					checkCount(true);
				} catch (Exception e) {
				}
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void findViews(View view) {
		tv_message = (TextView) view.findViewById(R.id.tv_message);
		llyt_zhaoshang = (LinearLayout) view.findViewById(R.id.llyt_zhaoshang);
		listview_zhaoshang = (NoScrollListView) view.findViewById(R.id.listview_zhaoshang);
		img_zhanqu = (ImageView) view.findViewById(R.id.img_zhanqu);
		rtly = (RelativeLayout) view.findViewById(R.id.rtly);
		tv_mingpai_title = (TextView) view.findViewById(R.id.tv_1_title);
//		view.findViewById(R.id.tv_11).setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				throw new RuntimeException("ceshi yisi ");
//			}
//		});
		// footview =
		// LayoutInflater.from(getActivity()).inflate(R.layout.item_footview,
		// null);
		// footViewTextView = (TextView) footview.findViewById(R.id.tv_message);
		llyt1 = (LinearLayout) view.findViewById(R.id.llyt_1);
		llyt2 = (LinearLayout) view.findViewById(R.id.llyt_2);
		llyt3 = (LinearLayout) view.findViewById(R.id.llyt_3);
		m_ViewFlow = (ViewFlow) view.findViewById(R.id.viewflow);
		m_redianLayout = (LinearLayout) view.findViewById(R.id.llyt);
		m_CircleFlowIndicator = (CircleFlowIndicator) view.findViewById(R.id.viewflowindic);
//		m_AutoTextView = (AutoTextView) view.findViewById(R.id.tv_6);
		m_AutoTextView_new = (TextSwitcher) view.findViewById(R.id.ts_fh_winner);
		m_AutoTextView_new.setFactory(new ViewSwitcher.ViewFactory() {
			@Override
			public View makeView() {
//               TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_textview, null);
				final TextView tv = new TextView(getActivity());
				tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
				tv.setTextColor(Color.BLACK);
				tv.setSingleLine(true);
				tv.setEllipsize(TextUtils.TruncateAt.END);
				tv.setGravity(Gravity.CENTER);
				tv.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
				return tv;
			}
		});
		m_GridView = (NoScrollGridView) view.findViewById(R.id.gridview);
		img_1 = (ImageView) view.findViewById(R.id.img_1);
		img_1.setOnClickListener(this);
		img_2 = (ImageView) view.findViewById(R.id.img_2);
		img_2.setOnClickListener(this);
		img_3 = (ImageView) view.findViewById(R.id.img_3);
		img_4 = (ImageView) view.findViewById(R.id.img_4);
		img_5 = (ImageView) view.findViewById(R.id.img_5);
		img_3.setOnClickListener(this);
		img_4.setOnClickListener(this);
		img_5.setOnClickListener(this);
		rtly_top = (RelativeLayout) view.findViewById(R.id.rtly_top);
		listview = (NoScrollListView) view.findViewById(R.id.listview);
		scrollview = (ObservableScrollView) view.findViewById(R.id.scrollview);
		scrollview.setScrollViewListener(this);
		edt_search = (TextView) view.findViewById(R.id.edt_search);
		tv_search = (TextView) view.findViewById(R.id.tv_search);
		btn_toTop = (ImageView) view.findViewById(R.id.btn_totop);
		btn_toTop.setOnClickListener(this);
		Lin_1 = (LinearLayout) view.findViewById(R.id.linear1);
		Lin_2 = (LinearLayout) view.findViewById(R.id.linear2);
		Lin_3 = (LinearLayout) view.findViewById(R.id.linear3);
		Lin_4 = (LinearLayout) view.findViewById(R.id.linear4);
		Lin_Btn = (LinearLayout) view.findViewById(R.id.lin_btn);
		initLinears();
		Lin_1.setOnClickListener(this);
		Lin_2.setOnClickListener(this);
		Lin_3.setOnClickListener(this);
		Lin_4.setOnClickListener(this);
		tv_search.setOnClickListener(this);
		edt_search.setOnClickListener(this);
		img_zhanqu.setOnClickListener(this);
		tv_message.setOnClickListener(this);
	}

	private void initLinears() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.BUTTONS);
		m_Http.sendRequest(getActivity(), true, map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject object = JSON.parseObject(result);
					JSONObject obj = JSON.parseObject(object.getString("retval"));
					Entry_Buttons entry1 = JSON.parseObject(obj.getString("ltp_list"), Entry_Buttons.class);
					Entry_Buttons entry2 = JSON.parseObject(obj.getString("factory_list"), Entry_Buttons.class);
					Entry_Buttons entry3 = JSON.parseObject(obj.getString("agent_list"), Entry_Buttons.class);
					Entry_Buttons entry4 = JSON.parseObject(obj.getString("gift_list"), Entry_Buttons.class);
					List<Entry_Buttons> datas = new ArrayList<Entry_Buttons>();
					datas.add(entry1);
					datas.add(entry2);
					datas.add(entry3);
					datas.add(entry4);
					LinearLayout Lin_item;
					ImageView Img_item;
					TextView Txt_item;
					Entry_Buttons entry;
					for (int i = 0; i < datas.size(); i++) {
						entry = datas.get(i);
						Lin_item = (LinearLayout) Lin_Btn.getChildAt(i);
						Img_item = (ImageView) Lin_item.getChildAt(0);
						Txt_item = (TextView) Lin_item.getChildAt(1);
						Txt_item.setText(entry.getName());
						ImageUtils.loadimg(Img_item, entry.getSign());
					}
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {
			}
		});

	}

	@Override
	public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
		if (y >= (m_ViewFlow.getHeight() - rtly_top.getHeight())) {
			changeTitleBarStatus(true);
		} else {
			changeTitleBarStatus(false);
		}

		int scrollY = scrollView.getScrollY();
		int height = scrollView.getHeight();
		int scrollViewMeasuredHeight = scrollView.getChildAt(0).getMeasuredHeight();
		if (scrollY == 0) {
			System.out.println("滑动到了顶端 view.getScrollY()=" + scrollY);
		}
		// if ((scrollY + height) == scrollViewMeasuredHeight) {
		// if (isLoading)
		// return;
		// isLoading = true;
		// Object object = footViewTextView.getTag();
		// if (object != null)
		// return;
		// m_CurrentPage++;
		// }
	}

	private void changeTitleBarStatus(boolean show) {
		if (show) {
			rtly_top.setBackgroundColor(Color.parseColor("#FF6600"));
			edt_search.setVisibility(View.VISIBLE);
			btn_toTop.setVisibility(View.VISIBLE);
		} else {
			rtly_top.setBackgroundColor(Color.TRANSPARENT);
			edt_search.setVisibility(View.GONE);
			btn_toTop.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(getActivity(), Act_Progects_List.class);
		TextView textview = null;
		String str = "";
		switch (v.getId()) {
			case R.id.btn_totop:
				scrollview.smoothScrollTo(0, 0);
				break;
			case R.id.linear1:
				textview = (TextView) Lin_1.getChildAt(1);
				str = textview.getText().toString();
				intent.putExtra("title", str);
				intent.putExtra("type", 1);
				startActivity(intent);
				break;
			case R.id.linear2:
				textview = (TextView) Lin_1.getChildAt(1);
				str = textview.getText().toString();
				intent.putExtra("title", str);
				intent.putExtra("type", 2);
				startActivity(intent);
				break;
			case R.id.linear3:
				intent = new Intent(getActivity(), Act_Dailipin.class);
				startActivity(intent);
				break;
			case R.id.linear4:
//				intent = new Intent(getActivity(), Act_KeTang.class);
//				startActivity(intent);
				textview = (TextView) Lin_4.getChildAt(1);
				str = textview.getText().toString();
				intent.putExtra("title", str);
				intent.putExtra("type", 4);
				startActivity(intent);
				break;
			case R.id.img_1:
				skipTehui(0);
				break;
			case R.id.img_2:
				skipTehui(1);
				break;
			case R.id.img_3:
				skipTehui(2);
				break;
			case R.id.img_4:
				ImageBanner imageBanner = twoPics.get(0);
				LinkTypeUtils util = new LinkTypeUtils(getActivity(), imageBanner.getAd_id(), imageBanner.getAd_link(), imageBanner.getAd_logo(),imageBanner.getAd_name(),imageBanner.getLink_type(),imageBanner.getLink_id(),imageBanner.getSpec_id());
				util.skip(false);
				break;
			case R.id.img_5:
				imageBanner = twoPics.get(1);
				util = new LinkTypeUtils(getActivity(), imageBanner.getAd_id(), imageBanner.getAd_link(), imageBanner.getAd_logo(),imageBanner.getAd_name(),imageBanner.getLink_type(),imageBanner.getLink_id(),imageBanner.getSpec_id());
				util.skip(false);
				break;
			case R.id.tv_search:
			case R.id.edt_search:
				intent = new Intent(getActivity(), Act_Search.class);
				startActivity(intent);
				break;
			case R.id.img_zhanqu:
				intent = new Intent(getActivity(), Act_Zhanqu.class);
				startActivity(intent);
				break;
			case R.id.tv_message:
				SkipUtils.toChat(getActivity(), null);
				break;
			case R.id.ts_fh_winner:
				int pos = currentSize - 1;
				ZhyHot hot = zhyHosts.get((pos % zhyHosts.size()));
				util = new LinkTypeUtils(getActivity(), "", hot.getS_link(), "", hot.getS_title(), hot.getLink_type(), hot.getLink_id(), hot.getSpec_id());
				util.skip(false);
				break;
		}
	}
	private void skipTehui(int pos){
		FristPagetodaytehui tehui = tehuis.get(pos);
		LinkTypeUtils utils = new LinkTypeUtils(getActivity(), "", tehui.getS_link(), tehui.getS_pic(),tehui.getS_title(),tehui.getLink_type(),tehui.getLink_id(), tehui.getSpec_id());
		utils.skip(false);
	}

	private List<ImageBanner> ImageBanners;

	/**
	 * 获取轮播图
	 */
	private void getBanber() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.sybanber);
		m_Http.sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				List<ImageBanner> list = JSON.parseArray(jsonObject.getString("retval"), ImageBanner.class);
				LogUtil.e(list.toString());
				ImageBanners = list;
				imageadapter = new ImageAdapter(getActivity(), list, rtly);
				m_ViewFlow.setAdapter(imageadapter);
				m_ViewFlow.setViewGroup(scrollview);
				m_ViewFlow.setTimeSpan(5000);
				m_ViewFlow.setFlowIndicator(m_CircleFlowIndicator);
				m_ViewFlow.setSideBuffer(list.size());
				m_ViewFlow.startAutoFlowTimer();
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private int currentSize = 0;
	private Handler handler = new Handler();
	private List<ZhyHot> zhyHosts;

	private void getTextBanner() {
		currentSize = 0;
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.zhyhot);
		m_Http.sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				zhyHosts = JSON.parseArray(jsonObject.getString("retval"), ZhyHot.class);
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {
						m_AutoTextView_new.setText(zhyHosts.get(currentSize++ % zhyHosts.size()).getS_title());
						handler.postDelayed(this, 2000);
					}
				}, 2000);


//				m_AutoTextView.setShowTexts(list);
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void getMingpai() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.sybrand);
		m_Http.sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				List<FristPageMingpai> list = JSON.parseArray(jsonObject.getString("retval"), FristPageMingpai.class);
				m_GridAdapter.addAll(list);
				if (list.size() == 0) {
					tv_mingpai_title.setVisibility(View.GONE);
				}
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void getList() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.admore);
		m_Http.sendRequest(getActivity(), map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				List<FristPageList> list = JSON.parseArray(jsonObject.getString("retval"), FristPageList.class);
				adapter.addAll(list);
				m_CurrentCount++;
				checkCount(true);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void checkCount(boolean smooth) {
		LogUtil.e("m_CurrentCount=" + m_CurrentCount);
//		if (m_CurrentCount >= INTERFACECOUNT) {
//			// ToolLoading.closeLoading(getActivity());
//			if (smooth) {
//				new Handler().post(new Runnable() {
//
//					@Override
//					public void run() {
//						// TODO Auto-generated method stub
//						LogUtil.e("scrollview-------------------");
		scrollview.smoothScrollTo(0, 0);
//					}
//				});
//			}
//
//		}
	}

	private List<FristPagetodaytehui> tehuis;
	private void gettodaytehui() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.FRIST_VALUE, HttpConst.SECOND_KEY,
				HttpConst.todaytehui);
		m_Http.sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSONObject.parseObject(result);
				tehuis = JSON.parseArray(jsonObject.getString("retval"),
						FristPagetodaytehui.class);
				try {
					ImageUtils.loadimg(img_1, tehuis.get(0).getS_pic(), new OnImageLoadListener() {

						@Override
						public void onLoadSuccess(Drawable drawable) {
							d1 = drawable;
							checkHight();
						}
					});
					ImageUtils.loadimg(img_2, tehuis.get(1).getS_pic(), new OnImageLoadListener() {

						@Override
						public void onLoadSuccess(Drawable drawable) {
							d2 = drawable;
							checkHight();
						}
					});
					ImageUtils.loadimg(img_3, tehuis.get(2).getS_pic(), new OnImageLoadListener() {

						@Override
						public void onLoadSuccess(Drawable drawable) {
							d3 = drawable;
							checkHight();
						}

					});
				} catch (Exception e) {
				}
			}

			private void checkHight() {
				if (d1 != null && d2 != null & d3 != null) {
					int d1NewHight = d1.getIntrinsicHeight() * ScreenUtil.getInstant().getDisWidth(getActivity()) / 2
							/ d1.getIntrinsicWidth();
					int d2NewHight = d2.getIntrinsicHeight() * ScreenUtil.getInstant().getDisWidth(getActivity()) / 2
							/ d2.getIntrinsicWidth();
					int d3NewHight = d3.getIntrinsicHeight() * ScreenUtil.getInstant().getDisWidth(getActivity()) / 2
							/ d3.getIntrinsicWidth();
					Hight = Math.max(d2NewHight + d3NewHight, d1NewHight);
					LayoutParams params = (LayoutParams) m_redianLayout.getLayoutParams();
					params.height = Hight;
					m_redianLayout.setLayoutParams(params);
					params = (LayoutParams) llyt1.getLayoutParams();
					params.weight = 1;
					params.height = Hight;
					llyt1.setLayoutParams(params);
					params = (LayoutParams) img_1.getLayoutParams();
					params.width = ScreenUtil.getInstant().getDisWidth(getActivity()) / 2;
					params.height = d1NewHight;
					params.topMargin = (Hight - d1NewHight) / 2;
					params.gravity = Gravity.CENTER;
					img_1.setLayoutParams(params);
					params = (LayoutParams) llyt2.getLayoutParams();
					params.weight = 1;
					params.height = Hight / 2;
					params.gravity = Gravity.CENTER_VERTICAL;
					llyt2.setLayoutParams(params);
					params = (LayoutParams) img_2.getLayoutParams();
					params.width = ScreenUtil.getInstant().getDisWidth(getActivity()) / 2;
					params.height = d2NewHight;
					params.gravity = Gravity.CENTER_VERTICAL;
					img_2.setLayoutParams(params);
					params = (LayoutParams) llyt3.getLayoutParams();
					params.weight = 1;
					params.height = Hight / 2;
					params.gravity = Gravity.CENTER_VERTICAL;
					llyt3.setLayoutParams(params);
					params = (LayoutParams) img_3.getLayoutParams();
					params.width = ScreenUtil.getInstant().getDisWidth(getActivity()) / 2;
					params.height = d3NewHight;
					params.gravity = Gravity.CENTER_VERTICAL;
					img_3.setLayoutParams(params);
					m_CurrentCount++;
					checkCount(true);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		FristPageList banner = null;
		if (parent.equals(listview)) {
			if (position == adapter.getCount())
				return;
			banner = adapter.getItem(position);
		} else if (parent.equals(listview_zhaoshang)) {
			if (position == adapter_zhaoshang.getCount())
				return;
			banner = adapter_zhaoshang.getItem(position);
		}
		if (banner == null) return;
		LinkTypeUtils util = new LinkTypeUtils(getActivity(), "", banner.getAd_link(), banner.getAd_logo(), banner.getAd_name(), banner.getLink_type(), banner.getLink_id(), banner.getSpec_id());
		util.skip(false);
	}

}
